package com.mxy.myapplication.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.BitmapEncoder;
import com.mxy.myapplication.R;
import com.mxy.myapplication.activity.MainActivity;
import com.mxy.myapplication.pojo.Pojo1;
import com.mxy.myapplication.util.itemtouchhelperUtil.ItemTouchHelperAdapter;
import com.mxy.myapplication.util.itemtouchhelperUtil.ItemTouchHelperViewHolder;

import java.util.ArrayList;
import java.util.Collections;

/**
 * 线性布局RecyclerView适配器
 */

public class AdapterForRv_Linear_ItemTouchHelper extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements ItemTouchHelperAdapter{

    private Context cont;
    private LayoutInflater lf;
    private ArrayList<Pojo1> data;
    private long systime;
    private SwipeRefreshLayout swipeRefreshLayout;

    public AdapterForRv_Linear_ItemTouchHelper(Context cont, ArrayList<Pojo1> data,SwipeRefreshLayout swipeRefreshLayout){
        this.cont=cont;
        this.lf=LayoutInflater.from(cont);
        this.data=data;
        this.swipeRefreshLayout=swipeRefreshLayout;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(this.lf.inflate(R.layout.item_recyclerview_linear_itemtouchhelper,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder=(MyViewHolder) holder;
        myViewHolder.mainlayout.setOnClickListener(new onclickListener(position,myViewHolder.iv));

        Glide.with(cont)
                .load(data.get(position).drawableID)
                .asBitmap()
                .encoder(new BitmapEncoder(Bitmap.CompressFormat.PNG,100))
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .dontAnimate()
                .animate(R.anim.alpha)
                .into(myViewHolder.iv);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        Collections.swap(data,fromPosition,toPosition);
        notifyItemMoved(fromPosition,toPosition);
        return true;
    }

    @Override
    public void onItemDismiss(int position) {
        data.remove(position);
        notifyItemRemoved(position);
    }

    /**
     * 自定义ViewHolder
     */
    private class MyViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder{

        ImageView iv;
        LinearLayout mainlayout;

        private MyViewHolder(View itemView) {
            super(itemView);
            iv=(ImageView) itemView.findViewById(R.id.imageview);
            mainlayout=(LinearLayout) itemView.findViewById(R.id.mainlayout);
        }

        @Override
        public void onItemSelected() {
            swipeRefreshLayout.setEnabled(false);
            int scale=(int)(5*cont.getResources().getDisplayMetrics().density);
            iv.setPadding(scale,scale,scale,scale);
        }

        @Override
        public void onItemClear() {
            swipeRefreshLayout.setEnabled(true);
            iv.setPadding(0,0,0,0);
        }
    }

    /**
     * 为item增加点击事件
     */
    private class onclickListener implements View.OnClickListener{

        private int position;
        private ImageView imageView;

        private onclickListener(int position,ImageView imageView){
            this.position=position;
            this.imageView=imageView;
        }

        @Override
        public void onClick(View v) {
            long clicktime=System.currentTimeMillis();
            if(clicktime-systime>500){//防止连点
                systime=clicktime;
                MainActivity mainActivity=(MainActivity)cont;
                mainActivity.setPopupWindow(imageView);
            }
        }
    }
}